home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 71247.3221@compuserve.com (Don Wallace)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ gurus, I have a question for you!
- Date: Tue, 20 Feb 1996 07:17:08 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4gbi49$38m@dub-news-svc-1.compuserve.com>
- References: <31281804.687F@iglou.com>
- NNTP-Posting-Host: hd48-175.compuserve.com
- X-Newsreader: Forte Free Agent v0.55
-
- "Abe L. Getchell" <panther@iglou.com> wrote:
-
- >How is it possible to check if a variable has been
- >initialized yet? If I do 'if ( !a ) { int a = 1 }' I get
- >an error message. Please, any help would be great. Reply
- >to the group or in mail. Thanks in advance!
-
- >Abe L. Getchell
-
- Abe,
-
- There are two kinds of variable storage in C++: global or
- extern-class, which are variables declared outside a function; and
- automatic ('local') variables, those declared inside a function, whose
- lifetime is one call of a function.
-
- A global/extern variable is always initialized to binary 0's when a
- program starts. Its lifetime is the life of the program.
-
- An automatic variable is not initialized to anything when its function
- is run. It will contain random garbage if it is not set to anything
- explicitly.
-
- But I also see a major problem with your understanding of variable
- scope in the code you supply:
-
- if ( !a )
- {
- int a = 1;
- }
-
- The code above is declaring a new variable also named 'a' whose scope
- is within the braces. This new 'a' inside the brackets is completely
- different than the 'a' you are testing in the if(!a).
-
- Just initialize your global or auto variables to a known value before
- trying to test or use them. And picking up a beginning book on 'C'
- wouldn't hurt either...
-
- - Don
-
-